package com.jic.product.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.ProductUnitsRelation;
import com.jic.product.manage.response.ProductUnitsRelationResponse;
import com.jic.product.mapper.ProductUnitsRelationMapper;
import com.jic.product.service.ProductUnitsRelationService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品计量单位关联表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-02-08 03:38:46
 */
@Service
@Slf4j
public class ProductUnitsRelationServiceImpl implements ProductUnitsRelationService {

    @Autowired
    private ProductUnitsRelationMapper productUnitsRelationMapper;

    @Override
    public RestResult<String> insert(ProductUnitsRelation productUnitsRelation) {
        try {
            Long unitsCount = productUnitsRelationMapper.queryProductUnitsCount(productUnitsRelation);
            if (unitsCount != null && unitsCount > 0) {
                return RestResult.error("9999", "辅计量单位已存在!");
            }
            productUnitsRelationMapper.insert(productUnitsRelation);
        } catch (Exception e) {
            log.error("新增商品计量单位失败:{}", e);
            return RestResult.error("9999", "新增商品计量单位失败!");
        }
        return RestResult.success("新增商品计量单位成功!");
    }

    @Override
    public RestResult<String> update(ProductUnitsRelation productUnitsRelation) {
        try {
            Long unitsCount = productUnitsRelationMapper.queryProductUnitsCount(productUnitsRelation);
            if (unitsCount != null && unitsCount > 0) {
                return RestResult.error("9999", "辅计量单位已存在!");
            }
            productUnitsRelationMapper.update(productUnitsRelation);
        } catch (Exception e) {
            log.error("修改商品计量单位失败:{}", e);
            return RestResult.error("9999", "修改商品计量单位失败!");
        }
        return RestResult.success("修改商品计量单位成功!");
    }

    @Override
    public long delete(Long id) {
        return productUnitsRelationMapper.delete(id);
    }

    @Override
    public ProductUnitsRelation queryByPrimaryKey(Long id) {
        return productUnitsRelationMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ProductUnitsRelationResponse> query(long productId) {
        return productUnitsRelationMapper.query(productId);
    }

    @Override
    public PageResult<ProductUnitsRelation> queryPage(ProductUnitsRelation productUnitsRelation, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductUnitsRelation> list = productUnitsRelationMapper.queryPage(startIndex, pageQuery.getLimit(), productUnitsRelation);
        long count = productUnitsRelationMapper.queryPageCount(productUnitsRelation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}
